package com.ziyueyuwen.xiniao.controller.zyclass;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ao.ClassInfoAO;
import com.ziyueyuwen.xiniao.service.ClassService;
import com.ziyueyuwen.xiniao.service.ClassUserService;
import com.ziyueyuwen.xiniao.bean.student.AddOrDeleteStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;

/**
 * @author wancheng  on 2018/12/18.
 */
@Validated
@Controller
@RequestMapping("/class/user")
public class ClassUserController {

    @Autowired
    ClassUserService classUserService;
    @Autowired
    ClassService classService;

    @RequestMapping(value = "/add_or_delete_student.do",method = RequestMethod.POST)
    public void addOrDeleteStudent(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @Validated AddOrDeleteStudent addOrDeleteStudent) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加或删除学生");
        try {
            classUserService.addOrDeleteStudent(addOrDeleteStudent.getClassId(), addOrDeleteStudent.getStudentAccounts());
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_class_info_and_user.do",method = RequestMethod.POST)
    public void updateClassInfoAndUser(HttpServletRequest request,
                                       HttpServletResponse response,
                                       ClassInfoAO classInfoAO) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改班级信息及人员");
        try {
            classService.updateClass(classInfoAO.getClassId(),
                    classInfoAO.getClassName(),
                    classInfoAO.getTeacherAccount(),
                    classInfoAO.getMasterAccount(),
                    classInfoAO.getStudentNum());
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
